<div class="subhead ">
  <div class="container mx-auto">
    <h5>~/account/edit</h5>
    <div class="log-settings">
      <ul>
        <li><%= link to: Routes.access_tokens_path(@conn, :index) do %><i class="fas fa-key"></i><span class="hide-on-mobile"> Manage access tokens</span><% end %></li>
        <li><%= link to: Routes.oauth_authorized_application_path(@conn, :index) do %><i class="fas fa-window-maximize "></i><span class="hide-on-mobile"> Authorized apps</span><% end %></li>
      </ul>
    </div>
  </div>
</div>
<div class="content container mx-auto">
  <h4 class="header-margin">System monitoring</h4>
  <div class="sub-form">
    <%= form_for @changeset, Routes.user_path(@conn, :update), fn user -> %>
      <div class="form-group">
        <%= checkbox user, :system_monitoring %>
        <%= label user, :system_monitoring, "Enable system monitoring", class: "label-padding" %>
        <small class="form-text text-muted">
          Events related to this account will be passed to dedicated system-provided sources.
        </small>
      </div>
      <%= submit "Update preferences", class: "btn btn-primary form-button" %>
    <% end %>
  </div>
</div>
<div class="content container mx-auto">
  <h4 class="header-margin">Account Owner Preferences</h4>
  <p>Preferences for the Logflare account owner.</p>
  <div class="sub-form">
    <%= section_header("Contact Information") %>
    <%= form_for @changeset, Routes.user_path(@conn, :update), fn f -> %>
    <div class="form-group">
      <%= label f, :name, class: "label-padding"  %>
      <%= text_input f, :name, placeholder: "Name", class: "form-control form-control-margin"%>
      <%= error_tag f, :name %>
      <%= label f, :company, "Company", class: "label-padding" %>
      <%= text_input f, :company, placeholder: "Company", class: "form-control form-control-margin"%>
      <%= error_tag f, :company %>
      <%= label f, :email_preferred, "Preferred email", class: "label-padding" %>
      <%= email_input f, :email_preferred, placeholder: "Preferred email", class: "form-control form-control-margin"%>
      <small class="form-text text-muted">
        Can be different from the account you authorized with. We'll only send you important product or account related emails.
      </small>
      <%= error_tag f, :email_preferred %>
      <%= label f, :phone, "Phone number", class: "label-padding" %>
      <%= text_input f, :phone, placeholder: "+15558675309", class: "form-control form-control-margin"%>
      <small class="form-text text-muted">
        Used to send you SMS alerts if enabled on a source.
      </small>
      <%= label f, :email_me_product, "Email me product updates", class: "label-padding" %>
      <%= checkbox f, :email_me_product %>
    </div>
    <%= submit "Update preferences", class: "btn btn-primary form-button" %>
  <% end %>
  <%= section_header("Signed In with #{String.capitalize(@user.provider)}") %>
  <p>You signed in with your <code><%= @user.provider %></code> account <code><%= @user.email %></code>.</p>
  <%= if @user.provider == "github" do %>
    <p>Review your authorized Oauth applications at Github.</p>
    <%= link "Visit Github", to: "https://github.com/settings/applications", class: "btn btn-primary form-button" %>
  <% end %>
  <%= if @user.provider == "google" do %>
    <p>Review apps with access to your Google account</a>.</p>
  <%= link "Visit Google", to: "https://myaccount.google.com/permissions?authuser=#{@user.email}", class: "btn btn-primary form-button" %>
<% end %>
</div>

<h4 id="billing-account" class="header-margin">Billing Account</h4>
<p>Create a billing account to subscribe to Logflare. Creating a billing account doesn't obligate you to anything. It's where you can update your billing preferences and manage your subscription.</p>
<p><i class="fas fa-exclamation-circle"></i> All team members can now access the billing account. You can invite someone in accounting as a team member to subscribe to Logflare if needed.</p>
<div class="sub-form">
<%= if @user.billing_account do %>
  <%= section_header("Edit Billing Account") %>
  <%= link "Edit billing account", to: ~p"/billing/edit", class: "btn btn-primary form-button" %>
<% else %>
  <%= section_header("Create a Billing Account") %>
  <%= link "Create a billing account", to: Routes.billing_path(@conn, :create), method: :post, class: "btn btn-primary form-button" %>
<% end %>
</div>

<h4 class="header-margin">Team Preferences</h4>
<p>Setup your team.</p>
<div class="sub-form">
  <%= section_header("Team Name") %>
  <%= form_for @changeset, Routes.user_path(@conn, :update), fn a -> %>
  <div class="form-group">
    <%= inputs_for a, :team, fn team -> %>
    <%= text_input team, :name, placeholder: "Wobbly Wombats", class: "form-control form-control-margin"%>
    <%= error_tag team, :name %>
  <% end %>
</div>
<%= submit "Update team", class: "btn btn-primary form-button" %>
<% end %>
<%= section_header("Team Members") %>
<%= render(LogflareWeb.SharedView, "team_members.html", assigns) %>
<%= section_header("Invite Team Members") %>
<div class="form-group">
  <p>Team members can delete and edit sources. They cannot edit account settings or manage team members. Use this link to invite team members. This link will expire 24 hours from now.</p>
  <code><%= Routes.auth_url(@conn, :login, invite_token: Logflare.Auth.gen_email_token(@team.id)) %></code>
</div>
<button class="btn btn-primary form-button" id="copy-path" data-clipboard-text="<%= Routes.auth_url(@conn, :login, invite_token: Logflare.Auth.gen_email_token(@team.id)) %>" data-toggle="tooltip" data-placement="top" title="<span id=&quot;copy-tooltip&quot;>Copy this</span>">Copy invite link</button>
</div>

<h4 class="header-margin">Account Preferences</h4>
<p>Set your account wide preferences.</p>
<div class="sub-form">
  <%= section_header("BigQuery Backend") %>
  <p>Set your Google Cloud Platform project id to setup your own BigQuery tables. Make sure to add our service account (<code><%= @service_account %></code>) to IAM with BigQuery Data Owner and BigQuery Job User permissions. See <%= link "this guide", to: Routes.marketing_path(@conn, :big_query_setup) %> for more info.</p>
  <%= render(LogflareWeb.UserView, "edit_bq_form.html", conn: @conn, changeset: @changeset) %>
  <%= section_header("Change Account Owner") %>
  <p>To change the main login of your Logflare account you must change the account owner. Add another team member first and then change the account owner here.</p>
  <div class="form-group">
    <%= form_for @changeset, Routes.user_path(@conn, :change_owner), fn owner -> %>
    <%= select owner, :team_user_id, Enum.map(@team.team_users, &{&1.email, &1.id}), prompt: "Select a team member", class: "form-control" %>
  </div>
  <%= submit "Change owner", class: "btn btn-danger form-button" %>
  <% end %>

  <%= section_header("Delete Your Account") %>
  <p>This will immediately delete everything associated with your account. There is no undo.</p>
  <%= link "Delete now", to: Routes.user_path(@conn, :delete), class: "btn btn-danger form-button", method: :delete %>
  <%= section_header("Reset API Key") %>
  <p>Immediately resest your API key. All future requests with your old API key will be rejected.</p>
  <%= link "Reset now", to: Routes.user_path(@conn, :new_api_key), class: "btn btn-danger form-button" %></li>
</div>
</div>
<script>
  document.addEventListener("DOMContentLoaded", async () => {
      User.initEditPage()
    })
</script>
